    //
//  ViewController.swift
//  weather
//
//  Created by 陈思斌 on 21/8/17.
//  Copyright © 2017年 陈思斌. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var mainViewController : UIViewController?
    var leftTableViewController: LeftTableViewController?
    var rightTableViewController:RightTableViewController?
    //什么时候显示中间 左边 右边的视图
    var transitionCondition: CGFloat?
    override var prefersStatusBarHidden: Bool{
        return true
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.transitionCondition = 0
        self.leftTableViewController = LeftTableViewController()
        self.rightTableViewController = RightTableViewController()
        let rootViewController = MainViewController()
        self.mainViewController  = UINavigationController(rootViewController: rootViewController)
        rootViewController.rootController = self
        self.rightTableViewController?.controller = self
        self.view.addSubview((self.leftTableViewController?.view)!)
        self.view.addSubview((self.rightTableViewController?.view)!)
        self.view.addSubview((self.mainViewController?.view)!)

        self.leftTableViewController?.view.isHidden = true
        self.rightTableViewController?.view.isHidden = true
        
        let pan = UIPanGestureRecognizer(target: self, action: #selector(panAction))
        self.mainViewController?.view.addGestureRecognizer(pan)

        
        NotificationCenter.default.addObserver(self, selector: #selector(showMainViewAction), name: NSNotification.Name(rawValue: AutoLocationNotification), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(showMainViewAction), name: NSNotification.Name(rawValue:"ChooseLocationCityNotification"), object: nil)

        
        
        // Do any additional setup after loading the view.
    }
    func showMainViewAction(sender:Notification){
        showMainView()
    }
    func panAction(sender:UIPanGestureRecognizer){
        
        let point = sender.translation(in: sender.view)
        self.transitionCondition = point.x * 0.5 + self.transitionCondition!
        
        let centerX = sender.view?.center.x
        let centerY = sender.view?.center.y
        if (sender.view?.frame.origin.x)! >= CGFloat(0){
            sender.view?.center = CGPoint(x: centerX! + point.x * 0.5 , y: centerY!)
            sender.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
            self.rightTableViewController?.view.isHidden = true
            self.leftTableViewController?.view.isHidden = false
        }else{
            sender.view?.center = CGPoint(x: centerX! + point.x * 0.5, y: centerY!)
            sender.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
            self.rightTableViewController?.view.isHidden = false
            self.leftTableViewController?.view.isHidden = true
        }
        
        if sender.state == .ended {
            if self.transitionCondition! > 80{
                self.showLeftView()
            }else if self.transitionCondition! < -80{
                self.showRightView()
            }else{
                self.showMainView()
            }
        }
        
        
    }
    
    func showMainView(){
        UIView.animate(withDuration: 0.6) {
            self.mainViewController?.view.center = CGPoint(x: UIScreen.main.bounds.size.width/2 , y: UIScreen.main.bounds.size.height/2)
        }
    }
    func showLeftView(){
        UIView.beginAnimations(nil, context: nil)
        self.mainViewController?.view.center =  CGPoint(x: UIScreen.main.bounds.size.width * 1.5 - 70, y: UIScreen.main.bounds.size.height/2)
        UIView.commitAnimations()
    }
    func showRightView(){
        UIView.beginAnimations(nil, context: nil)
        self.mainViewController?.view.center = CGPoint(x: 70 - UIScreen.main.bounds.size.width/2, y: UIScreen.main.bounds.size.height/2)
        UIView.commitAnimations()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
